package com.wash.shoes.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.common.enums.FactoryShelvesStatusEnum;
import com.wash.shoes.entity.FactoryshelvesRackGridPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.mapper.CustomerMapper;
import com.wash.shoes.mapper.FactoryshelvesRackGridMapper;
import com.wash.shoes.mapper.OrderMapper;
import com.wash.shoes.service.FactoryshelvesRackGridService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FactoryshelvesRackGridServiceImpl extends
        ServiceImpl<FactoryshelvesRackGridMapper, FactoryshelvesRackGridPo> implements FactoryshelvesRackGridService {

    /**
     * @param shoeNum
     * @param factoryId 工厂id
     * @return
     */
    @Override
    public List<FactoryshelvesRackGridPo> shareGridList(Integer shoeNum, String factoryId, String orderId) {
        // return baseMapper.shareGridList(shoeNum, shopId);
        long count = baseMapper.selectCount(
                new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryId, factoryId));
        Assert.isTrue(count > 0, () -> new WashException("该工厂未分配鞋架,factoryId:" + factoryId));

        //TODO 如果这个订单分配了鞋架就用原来的鞋架
        List<FactoryshelvesRackGridPo> data = baseMapper.selectList(new LambdaQueryWrapper<FactoryshelvesRackGridPo>()
                .eq(FactoryshelvesRackGridPo::getFactoryId, factoryId).eq(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.ONLINE)
                .eq(FactoryshelvesRackGridPo::getOrderId, orderId));

        if (CollectionUtil.isNotEmpty(data)) {
            return data;
        }

        // 查询上架的 并且未绑定订单号的
        count = baseMapper.selectCount(
                new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryId, factoryId)
                        .eq(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.ONLINE)
                        .isNull(FactoryshelvesRackGridPo::getOrderId));
        Assert.isTrue(count > 0, () -> new WashException("店铺鞋架已经用完,请仔细检查鞋架使用情况,factoryId:" + factoryId));
        return baseMapper.selectList(
                new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryId, factoryId)
                        .eq(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.ONLINE)
                        .isNull(FactoryshelvesRackGridPo::getOrderId).orderByAsc(FactoryshelvesRackGridPo::getSortNum)
                        .last("limit " + shoeNum));
    }

    /**
     * 查询列表
     *
     * @param factoryshelvesId 货架格子id
     * @param orderNo          订单号
     * @param phone            关键字 手机号
     * @return
     */
    @Override
    public Page<FactoryshelvesRackGridPo> queryListByParam(IPage page, String factoryshelvesId,
                                                           String orderNo, String phone, int lastNum) {
        //不带条件的走这种分页逻辑
        if (StringUtils.isEmpty(orderNo) && StringUtils.isEmpty(phone)) {
            List<FactoryshelvesRackGridPo> records = baseMapper.queryListByParamNoParam(factoryshelvesId, lastNum, page.getSize());
            long count = baseMapper.selectCount(new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryshelvesId, factoryshelvesId).eq(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.ONLINE));
            Page<FactoryshelvesRackGridPo> page1 = new Page<>();
            /*"total": 500,  总条数
            "size": 24,     当前结果数
            "current": 1,  当前页
            "pages": 21  一共有多少页*/
            page1.setRecords(records);
            page1.setPages((count + page.getSize() - 1) / page.getSize());
            page1.setCurrent(page.getCurrent());
            page1.setSize(page.getSize());
            page1.setTotal(count);
            return page1;
        }
        return getBaseMapper().queryListByParam(page, factoryshelvesId, orderNo, phone);
        //  List<FactoryshelvesRackGridPo>  data=  getBaseMapper().selectList(new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryshelvesId, factoryshelvesId).eq(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.ONLINE).orderByAsc(FactoryshelvesRackGridPo::getSortNum));
       /* if(CollectionUtil.isNotEmpty(data)){
            for( FactoryshelvesRackGridPo record: data){
                if(Objects.nonNull(record.getOrderId())){
                    OrderPo orderPo= orderMapper.selectById(record.getOrderId());
                    if(Objects.nonNull(orderPo)){
                        FactoryshelvesRackGridPo.CustomInfo customInfo = new FactoryshelvesRackGridPo.CustomInfo();
                        customInfo.setPhone(orderPo.getCustomPhone());
                        customInfo.setName(customerMapper.selectById(orderPo.getCustomId()).getName());
                        record.setCustomInfo(customInfo);
                    }
                }
            }
        }*/
//        return data;
    }

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 根据格子id释放绑定的订单号
     *
     * @param id 格子id
     * @return
     */
    @Override
    public int updateFactoryShevelsOrderIdWithId(@Param("id") String id) {
        if (StringUtils.isEmpty(id)) {
            Assert.isTrue(false, "格子id不能为空.");
            return 0;
        }
        return getBaseMapper().updateFactoryShevelsOrderIdWithId(id);
    }
}
